package com.sky.web.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页列表查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result findByPage(CategoryPageQueryDTO categoryPageQueryDTO){//1 接收请求

        //2 调用service
        PageBean pageBean = categoryService.findByPage(categoryPageQueryDTO);

        //3 返回结果
        return Result.success(pageBean);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Category category){//1 接收请求参数

        //2 调用Service
        categoryService.save(category);

        //3 返回结果
        return Result.success();
    }


    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteById(Long id){//1 接收参数
        //2 调用service
        categoryService.delete(id);

        //3 返回结果
        return Result.success();
    }


    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Category category){//1 接收参数
        //2 调用Service
        categoryService.update(category);

        //3 返回结果
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result findByType(Integer type){ //1 接收参数

        //2 调用Service
        List<Category> list = categoryService.findByType(type);

        //3 返回结果
        return Result.success(list);
    }

    /**
     * 启用禁用分类
     */
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id){
        //1 接收参数  (路径参数 @PathVariable)

        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();
        //2 调用Service
        categoryService.startOrStop(category);
        //3 返回结果
        return Result.success();
    }
}
